/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.dao;

import org.ipti.br.tag.entity.AddInfoPersonage;
import org.ipti.br.tag.entity.AddInfoPersonage_;
import org.ipti.br.tag.entity.AdditionalInfoType;
import org.ipti.br.tag.entity.AdditionalInfoType_;
import org.ipti.br.tag.entity.Personage;
import org.ipti.br.tag.entity.Personage_;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.Root;
import org.ipti.br.tag.util.EncryptionUtil;

/**
 *
 * @author root
 */
@Stateless
public class AdditionalInfoTypeFacade extends AbstractFacade<AdditionalInfoType> {

    @PersistenceContext(unitName = "TAG2PU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public AdditionalInfoTypeFacade() {
        super(AdditionalInfoType.class);
    }

    @Override
    public void create(AdditionalInfoType additionalInfoType) {
        getEntityManager().persist(additionalInfoType);
    }

    @Override
    public void edit(AdditionalInfoType additionalInfoType) {
        additionalInfoType.setSerialNumber(null);

        getEntityManager().merge(additionalInfoType);
    }

    public void editForSynchronization(AdditionalInfoType additionalInfoType) {
        getEntityManager().merge(additionalInfoType);
    }

    public List<AdditionalInfoType> findAllOrder() {
        return findAllOrderBy(AdditionalInfoType_.acronym);
    }
    
    public List<AdditionalInfoType> findAddInfoTypesPerPersonage(Personage personage) {
        CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
        CriteriaQuery<AdditionalInfoType> cq = cb.createQuery(AdditionalInfoType.class);
        Root<AddInfoPersonage> root = cq.from(AddInfoPersonage.class);
        Join<AddInfoPersonage, AdditionalInfoType> addInfoTypes = root.join(AddInfoPersonage_.additionalInfoType);
        cq.select(addInfoTypes);
        
        cq.where(cb.and(cb.equal(root.join(AddInfoPersonage_.personage).get(Personage_.id), personage.getId())));
        return getEntityManager().createQuery(cq).getResultList();
    }
}
